package cc.mcggdd.leah.boot.stock.controller;

import cc.mcggdd.leah.boot.stock.service.StockHoldingService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cc.mcggdd.leah.boot.stock.model.form.StockHoldingForm;
import cc.mcggdd.leah.boot.stock.model.query.StockHoldingQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockHoldingVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cc.mcggdd.leah.boot.common.result.PageResult;
import cc.mcggdd.leah.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 持仓管理前端控制层
 *
 * @author 
 * @since 2025-02-13 09:38
 */
@Tag(name = "持仓管理接口")
@RestController
@RequestMapping("/api/v1/stockHoldings")
@RequiredArgsConstructor
public class StockHoldingController  {

    private final StockHoldingService stockHoldingService;

    @Operation(summary = "持仓管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('stock:stockHolding:query')")
    public PageResult<StockHoldingVO> getStockHoldingPage(StockHoldingQuery queryParams ) {
        IPage<StockHoldingVO> result = stockHoldingService.getStockHoldingPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增持仓管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('stock:stockHolding:add')")
    public Result<Long> saveStockHolding(@RequestBody @Valid StockHoldingForm formData ) {
        Long result = stockHoldingService.saveStockHolding(formData);
        return Result.success(result);
    }

    @Operation(summary = "获取持仓管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('stock:stockHolding:edit')")
    public Result<StockHoldingForm> getStockHoldingForm(
        @Parameter(description = "持仓管理ID") @PathVariable Long id
    ) {
        StockHoldingForm formData = stockHoldingService.getStockHoldingFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改持仓管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('stock:stockHolding:edit')")
    public Result<Void> updateStockHolding(
            @Parameter(description = "持仓管理ID") @PathVariable Long id,
            @RequestBody @Validated StockHoldingForm formData
    ) {
        boolean result = stockHoldingService.updateStockHolding(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除持仓管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('stock:stockHolding:delete')")
    public Result<Void> deleteStockHoldings(
        @Parameter(description = "持仓管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = stockHoldingService.deleteStockHoldings(ids);
        return Result.judge(result);
    }
}
